package Interface;

import static util.Constant.STR_PRESS_ENTER;

import java.util.Scanner;

public class Console {

    private static final String CARAC = "=";
    private static final int LARGEUR = 40;

    /**
     * Ask user for an integer
     * 
     * @return int given by the user
     */
    public static int askInt() {
        int saisie = 0;
        boolean continu = true;
        do {
            System.out.print(">>> ");
            Scanner sc = new Scanner(System.in);
            try {
                saisie = Integer.valueOf(sc.nextLine().trim());
                continu = (saisie < 0);
            } catch (Exception e) {
                continu = true;
            }
        } while (continu);
        return saisie;
    }

    /**
     * Ask user for a text, must not be empty
     * 
     * @return String given by the user
     */
    public static String askText() {
        return askText(false);
    }

    /**
     * Ask user for a text
     * 
     * @param b Could it be blank ?
     * 
     * @return String given by the user
     */
    public static String askText(boolean b) {
        String saisie;
        do {
            System.out.print(">>> ");
            Scanner sc = new Scanner(System.in);
            saisie = sc.nextLine().trim();
        } while ((saisie.isEmpty() && !b));
        return saisie;
    }

    /**
     * Ask confirmation to users
     */
    public static boolean confirm() {
        System.out.println("Confirm delete: (Y)es / Other key to cancel");
        System.out.print(">>>");
        String input = new Scanner(System.in).nextLine();
        return (null != input && input.equalsIgnoreCase("y"));
    }

    /**
     * Display an header
     * 
     * @param title The title
     */
    public static void displayHeader(String title) {
        line();
        int aGauche = Math.max(0, LARGEUR - title.length() - 2) / 2;
        System.out.print(CARAC);
        for (int i = 0; i < aGauche; i++) {
            System.out.print(" ");
        }
        System.out.print(title);
        int aDroite = Math.max(0, LARGEUR - aGauche - 2 - title.length());
        for (int i = 0; i < aDroite; i++) {
            System.out.print(" ");
        }
        System.out.print(CARAC);
        System.out.println();
    }

    /**
     * Display a menu with integer choices
     * 
     * @param choix An array of what user could choose
     */
    public static void displayMenu(String[] choix) {
        line();
        for (int i = 0; i < choix.length; i++) {
            System.out.printf(CARAC + " %3d. %-" + (LARGEUR - 10) + "s  " + CARAC + "\n", i + 1,
                    choix[i]);
        }
        line();
    }

    /**
     * Display a menu with first letter choices
     * 
     * @param choix An array of what user could choose
     */
    public static void displayMenuWithFirstLetters(String[] choix) {
        line();
        for (int i = 0; i < choix.length; i++) {
            System.out.printf(CARAC + " (%c)%-" + (LARGEUR - 8) + "s  " + CARAC + "\n", choix[i]
                    .charAt(0), choix[i].substring(1));
        }
        line();
    }

    /**
     * Display a pseudo-row on the standard output
     */
    public static void line() {
        for (int i = 0; i < LARGEUR; i++) {
            System.out.print(CARAC);
        }
        System.out.println();
    }
 
    /**
     * Ask user to press Enter to continue
     */
    public static void pause() {
        System.out.println(STR_PRESS_ENTER);
        new Scanner(System.in).nextLine();
    }

    
    /**
     * Ask if we try again
     */
    public static boolean tryAgain() {
        System.out.print("Try again? (Y/N): ");
        try {
            return new Scanner(System.in).nextLine().equalsIgnoreCase("y");
        } catch (Exception e) {
            return false;
        }
    }
}
